package com.kid.kidliving.commodity.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kid.common.valid.SaveGroup;
import com.kid.common.valid.UpdateGroup;
import com.kid.common.valid.UpdateIsShowFroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kid.kidliving.commodity.entity.BrandEntity;
import com.kid.kidliving.commodity.service.BrandService;
import com.kid.common.utils.PageUtils;
import com.kid.common.utils.R;


/**
 * 家居品牌
 *
 * @author kid
 * @email kid@gmail.com
 * @date 2025-09-08 23:58:01
 */
@RestController
@RequestMapping("commodity/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:brand:info")
    public R info(@PathVariable("id") Long id) {
        BrandEntity brand = brandService.getById(id);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     *
     * @Validated 启用BrandEntity校验规则
     * BindingResult：springboot会将校验错误放入BindingResult，错误可以通过map取出
     * @Validated({SaveGroup.class}) 表示在调用save方法进行参数校验时，使用SaveGroup校验规则
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:brand:save")
        public R save(@Validated({SaveGroup.class}) @RequestBody BrandEntity brand/*,
                BindingResult bindingResult*/) {
//        if (bindingResult.hasErrors()) {
//            //创建一个map，用于收集校验错误
//            Map<String, String> map = new HashMap<>();
//            //遍历bindingResult，将错误信息收集到map中
//            bindingResult.getFieldErrors().forEach((errorItem) -> {
//                //得到field
//                String field = errorItem.getField();
//                //得到错误校验信息
//                String defaultMessage = errorItem.getDefaultMessage();
//                map.put(field, defaultMessage);
//            });
//            return R.error(400, "品牌表单数据不合法").put("data", map);
//        } else {//没有校验错误，数据入库
//            brandService.save(brand);
//            return R.ok();
//        }
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     * @Validated({UpdateGroup.class}) 表示在调用save方法进行参数校验时，使用UpdateGroup校验规则
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    @RequestMapping("/update/isshow")
    //@RequiresPermissions("commodity:brand:update")
    public R updateIsShow(@Validated({UpdateIsShowFroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:brand:delete")
    public R delete(@RequestBody Long[] ids) {
        brandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/save2")
    //@RequiresPermissions("commodity:brand:save")
    public R save2() {
        int i=10/0;
        return R.ok();
    }

}
